<?php

namespace App\Http\Controllers;
use Illuminate\Contracts\Support\MessageBag;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Tasks;
use Validator;


class TaskController extends Controller
{
    public function addTask(Request $request)
    {
        $validator=Validator::make($request->all(), [
            'user_id' => 'required',
            'type' => 'required',
            'title'=>'required',
            'content'=>'required',
            'end_time'=>'required',
            'status'=>'required',
            'location'=>'required',
            'people_limit'=>'required',
        ]);
        if($validator->fails())
        {
            return self::result(false,$validator->errors(),null);
        }
        $task=Tasks::create($request->all());
        if($task->save())
        {
            return self::result(true,"创建成功",$task);
        }
        else
        {
            return self::result(false,"服务器爆炸了",null);
        }
    }

    public function getTaskById(Request $request)
    {
        $data=$request->all();
        if(!isset($data['id']))
        {
            return self::result(false,"请指定委托的ID",null);
        }
        $task=Tasks::find($data['id']);
        if($task!=null)
        {
            $task=$task->toArray();
        }
        return self::result(true,"查找成功",$task);

    }

    public function getTasks(Request $request)
    {
        //type 类型
        //page 第几页
        //count 每页的数量,从第1页开始
        //order 默认是时间顺序吧
        $validator=Validator::make($request->all(), [
            'type'=>'Integer',
            'page'=>'Integer|min:1|required_with_all:count',
            'count'=>'Integer|min:1|required_with_all:page',
        ]);
        if($validator->fails())
        {
            return self::result(false,"参数错误",null);
        }
        $data=$request->all();
        if(sizeof($data)==0)
        {
            return self::result(true,"获取委托列表成功",Tasks::all()->toArray());
        }
        $query=null;
        $res=[];
        if(isset($data['type']))
        {
            $query=Tasks::where('type',$data['type']);
        }
        if(isset($data['page'])&&isset($data['count']))
        {
            if($query!=null)
            {
                $res=$query->skip(($data['page']-1)*$data['count'])->take($data['count'])->get()->toArray();
            }
            else
            {
                $res=Tasks::skip(($data['page']-1)*$data['count'])->take($data['count'])->get()->toArray();
            }
        }
        else
        {
            $res=$query->get()->toArray();
        }
        return self::result(true,"获取委托列表成功",$res);
    }

    public function getTasksByUser(Request $request)
    {
        //获得用户所有的task
        $validator=Validator::make($request->all(), [
            'user_id'=>'required|Integer',
        ]);
        if($validator->fails())
        {
            return self::result(false,"参数错误",null);
        }
        $data=$request->all();
        $res=Tasks::where('user_id',$data['user_id'])->get()->toArray();
        return self::result(true,"获取信息成功",$res);
    }

    public function updateTask(Request $request)
    {
        $validator=Validator::make($request->all(), [
            'id'=>'required',
            'user_id' => 'Integer',
            'type' => 'required',
            'title'=>'required',
            'content'=>'required',
            'end_time'=>'required',
            'status'=>'required',
            'location'=>'required',
            'people_limit'=>'required',
        ]);
        if($validator->fails())
        {
            return self::result(false,"参数错误",null);
        }
        $data=$request->all();
        $task=Tasks::find($data['id']);
        if(!isset($task->id))
        {
            return self::result(false,"不存在该ID",null);
        }
        try{
            $task->update($data);
            return self::result(true,"更新成功",null);
        }
        catch (\Exception $e)
        {
            return self::result(false,"出错",$e->getMessage());
        }


    }

    public function deleteTaskById(Request $request)
    {
        $validator=Validator::make($request->all(), [
            'id'=>'required|Integer',
        ]);
        if($validator->fails())
        {
            return self::result(false,"参数错误",null);
        }
        $data=$request->all();
        Tasks::destroy($data['id']);
        return self::result(false,"删除成功",null);

    }

    public static function result($success,$info,$res)
    {
        return ['success'=>$success,'info'=>$info,'result'=>$res];
    }
}
